package com.yylending.las.lending.process.confirmloan;

import android.text.TextUtils;
import com.google.gson.Gson;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.secure.Cryptos;
import com.yylending.las.Global;
import com.yylending.las.common.AESEntity;
import com.yylending.las.lending.quota.entity.CustomerBean;
import com.yylending.las.lending.quota.idcard.IdCardModel;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by fan on 2017/6/16.
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class ConfirmLoanPresenter extends Presenter<ConfirmLoanModel, ConfirmLoanPresenter.View> {

	private static final String TAG = ConfirmLoanPresenter.class.getSimpleName();
	private static final String RET_CODE_SUBMIT_STATUS_CREDIT_EXPIRED = "100040"; // 征信过期
	private static final String RET_CODE_SUBMIT_STATUS_REQUEST_REPEAT = "100041"; // 重复请求

	public static class Generator implements PresenterFactory<ConfirmLoanPresenter> {
		@Override
		public ConfirmLoanPresenter create() {
			return new ConfirmLoanPresenter();
		}
	}

	public interface View extends IView {
		void confirmLoanSuccess(ConfirmLoanModel.ConfirmLoan confirmLoan);

		void confirmLoanFailure(String tips);

		/**
		 * 重复提交借款申请
		 * @param tips
		 */
		void confirmLoanRepeat(String tips);

		void creditReportingOutdated();

		void getIdCardSuccess(IdCardModel.IdCard idCard);

		void getIdCardFailure(String s);
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new ConfirmLoanModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	/**
	 * 借款申请
	 */
	public void confirmLoan(String loanTerm, String businessSum, String longitude, String latitude, String isOnline) {
		ConfirmLoanModel.ConfirmLoanParams params = new ConfirmLoanModel.ConfirmLoanParams();
		params.loanTerm = loanTerm;
		params.businessSum = businessSum;
		params.longitude = longitude;
		params.latitude = latitude;
		params.isOnline = isOnline;
		execute(new AESEntity(params), ConfirmLoanModel.METHOD_CONFIRM_LOAN, false, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getConfirmLoanMResult(ConfirmLoanModel.ConfirmLoanResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				mView.confirmLoanSuccess(result.getData());
			} else if (result.getRetCode().equals(RET_CODE_SUBMIT_STATUS_CREDIT_EXPIRED)) {
				mView.creditReportingOutdated();
			} else if (TextUtils.equals(RET_CODE_SUBMIT_STATUS_REQUEST_REPEAT, result.getRetCode())) {
				mView.confirmLoanRepeat(TextUtils.isEmpty(result.getRetMsg()) ? "请勿重复提交" : result.getRetMsg());
			} else {
				mView.confirmLoanFailure(TextUtils.isEmpty(result.getRetMsg()) ? "请求失败" : result.getRetMsg());
			}
		}
	}

	/**
	 * 获取身份证信息
	 */
	public void getIdCard(String type) {
		ConfirmLoanModel.IdCardParams params = new ConfirmLoanModel.IdCardParams();
		params.type=type;
		execute(params, ConfirmLoanModel.METHOD_GET_ID_CARD, true, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getIdCardResult(ConfirmLoanModel.IdCardResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result) && null != result.getData() && !TextUtils.isEmpty(result.getData().json)) {
				ConfirmLoanModel.AesData data = result.getData();
				CLog.i(TAG, "服务器基本信息encode=" + data.json);
				CustomerBean customerBean = new Gson().fromJson(Cryptos.aesDecrypt(data.json, Global.KEY_AES), CustomerBean.class);
				if (null != customerBean && null != customerBean.identity) {
					mView.getIdCardSuccess(customerBean.identity);
				} else {
					mView.getIdCardFailure("身份证信息不存在");
				}
			} else {
				String msg = result.getRetMsg();
				mView.getIdCardFailure(TextUtils.isEmpty(msg) ? "身份证信息不存在" : msg);
			}
		}
	}
}